package com.anp.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.anp.business.entity.ZhCertify;
import com.anp.business.entity.dto.CertifyResp;
import com.anp.business.repository.ZhCertifyRepository;
import com.anp.business.service.IBankBusinessService;
import com.anp.business.service.IZhCertifyService;
import com.anp.business.utils.DES3;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yicky zhang
 * @create 2018-09-11 上午11:10
 * @desc
 **/
@Service
public class ZhCertifyServiceImpl implements IZhCertifyService {
    private static final Logger logger = LoggerFactory.getLogger(ZhCertifyServiceImpl.class);

    @Autowired
    private ZhCertifyRepository zhCertifyRepository;
    @Autowired
    private IBankBusinessService bankBusinessService;

    @Override
    public List<ZhCertify> getLimit200(Integer limit) {
        return zhCertifyRepository.getLimit200(limit);
    }

    @Override
    public void doQuartz(Integer limit) {
        List<ZhCertify> limit200 = zhCertifyRepository.getLimit200(limit);
        limit200.forEach(zhCertify -> {
            try {
                if (StringUtils.isBlank(zhCertify.getCardNo())) {
                    logger.info("卡号为空，跳过.");
                    return;
                }
                if (StringUtils.isNotBlank(zhCertify.getName())) {
                    CertifyResp certify = bankBusinessService.certify("", DES3.decode(zhCertify.getCardNo()), DES3.decode(zhCertify.getName()), "", "");
                    logger.info("返回:" + JSON.toJSONString(certify));
                } else if (StringUtils.isNotBlank(zhCertify.getCertNo())) {
                    CertifyResp certify = bankBusinessService.certify("", DES3.decode(zhCertify.getCardNo()), "", "", DES3.decode(zhCertify.getCertNo()));
                    logger.info("返回:" + JSON.toJSONString(certify));
                } else if (StringUtils.isNotBlank(zhCertify.getPhoneNo())) {
                    CertifyResp certify = bankBusinessService.certify("", DES3.decode(zhCertify.getCardNo()), "", DES3.decode(zhCertify.getPhoneNo()), "");
                    logger.info("返回:" + JSON.toJSONString(certify));
                } else {
                    logger.error("姓名，身份证号，手机号都为空，跳过");
                }
            } catch (Exception e) {
                logger.error("银商校验错误", e);
            }
        });
    }
}
